package com.alstudio.view.toast;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.ui.base.BaseActivity;
import com.alstudio.utils.activity.ALActivityManager;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.listener.ViewEventListener;
import com.loovee.imaohu.R;

public class MyToastUtil {

	private static int toastBgId = R.drawable.tipline;
	private static String textColor = "#ffffff";
	private static ViewEventListener listener;
	private static View mView;
	private static MyToast mMytoast;
	private final static String TAG = "MyToastUtil";

	public static void setToastBgId(int id) {
		toastBgId = id;
	}

	public static void setTextColor(String color) {
		textColor = color;
	}

	public static void setToastEventListener(ViewEventListener l) {
		listener = l;
	}

	public static void showToastOnTop(String str, int time) {

		dismissToast();
		dismissToast(mMytoast);

		BaseActivity context = ALActivityManager.getALActivityManager()
				.getCurrentActivity();
		if (context == null) {
			return;
		}

		final TextView textControl = new TextView(context);
		textControl.setText(str);
		textControl.setBackgroundResource(toastBgId);
		textControl.setGravity(Gravity.CENTER_HORIZONTAL);
		textControl.setPadding(0, 8, 0, 0);
		textControl.setTextSize(18);
		textControl.setTextColor(Color.parseColor(textColor));

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		((Activity) context).addContentView(textControl, params);
		textControl.measure(0, 0);

		// 执行从上往下掉落的动画
		TranslateAnimation down = new TranslateAnimation(0, 0,
				-textControl.getMeasuredHeight(), 0);
		down.setDuration(500);
		// 执行由浅变深的透明变化
		AlphaAnimation fade_in = new AlphaAnimation(0.5f, 1);
		fade_in.setDuration(500);

		// 执行从下往上的动画
		TranslateAnimation up = new TranslateAnimation(0, 0, 0,
				-textControl.getMeasuredHeight());
		up.setDuration(500);
		// 此动画执行的时间即为该toast消失的时间
		up.setStartOffset(time - 500);
		// 执行由深变浅的动画
		AlphaAnimation fade_out = new AlphaAnimation(1, 0.5f);
		fade_out.setDuration(500);
		// 执行时间与up动画一致
		fade_out.setStartOffset(time - 500);

		AnimationSet as = new AnimationSet(true);
		as.addAnimation(down);
		as.addAnimation(fade_in);
		as.addAnimation(up);
		as.addAnimation(fade_out);
		as.setFillAfter(false);
		as.setAnimationListener(new Animation.AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				if (listener != null) {
					listener.onShow();
				}
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				textControl.setVisibility(View.GONE);
				if (listener != null) {
					listener.onDismiss();
				}
			}
		});
		mView = textControl;
		textControl.startAnimation(as);

	}

	public static void showToastOnTop(Context context, String str, int time) {

		dismissToast();
		dismissToast(mMytoast);
		final TextView textControl = new TextView(context);
		textControl.setText(str);
		textControl.setBackgroundResource(toastBgId);
		textControl.setGravity(Gravity.CENTER_HORIZONTAL);
		textControl.setPadding(0, 8, 0, 0);
		textControl.setTextSize(18);
		textControl.setTextColor(Color.parseColor(textColor));

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		// FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
		// LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		// // params.gravity = Gravity.CENTER;
		// params.gravity = Gravity.BOTTOM;

		((Activity) context).addContentView(textControl, params);
		textControl.measure(0, 0);

		// 执行从上往下掉落的动画
		TranslateAnimation down = new TranslateAnimation(0, 0,
				-textControl.getMeasuredHeight(), 0);
		down.setDuration(500);
		// 执行由浅变深的透明变化
		AlphaAnimation fade_in = new AlphaAnimation(0.5f, 1);
		fade_in.setDuration(500);

		// 执行从下往上的动画
		TranslateAnimation up = new TranslateAnimation(0, 0, 0,
				-textControl.getMeasuredHeight());
		up.setDuration(500);
		// 此动画执行的时间即为该toast消失的时间
		up.setStartOffset(time - 500);
		// 执行由深变浅的动画
		AlphaAnimation fade_out = new AlphaAnimation(1, 0.5f);
		fade_out.setDuration(500);
		// 执行时间与up动画一致
		fade_out.setStartOffset(time - 500);

		AnimationSet as = new AnimationSet(true);
		as.addAnimation(down);
		as.addAnimation(fade_in);
		as.addAnimation(up);
		as.addAnimation(fade_out);
		as.setFillAfter(false);
		as.setAnimationListener(new Animation.AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				if (listener != null) {
					listener.onShow();
				}
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				textControl.setVisibility(View.GONE);
				if (listener != null) {
					listener.onDismiss();
				}
			}
		});
		mView = textControl;
		textControl.startAnimation(as);

	}

	public static void dismissToast() {
		if (mView != null) {
			mView.clearAnimation();
			mView.setVisibility(View.GONE);
			if (listener != null) {
				listener.onDismiss();
			}
		}
	}

	public static void showCustomViewToast(MyToast toast, int gravit) {
		if (toast == null) {
			ALLog.e("error!your MyToast object is null!");
			return;
		}

		if (toast.getContext() == null) {
			ALLog.e("error!your context object is null!");
			return;
		}

		View v = toast.getView();
		Context context = toast.getContext();
		LinearLayout base = null;
		if (toast.getBaseLayour() == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			base = (LinearLayout) inflater.inflate(R.layout.base_toast_layout,
					null);
			toast.setBaseLayout(base);
		} else {
			base = (LinearLayout) toast.getBaseLayour();
		}

		base.addView(v);

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = gravit;

		((Activity) context).addContentView(base, params);
		AnimationSet as = toast.getAnimationSet();

		if (as == null) {
			as = new AnimationSet(true);
			// 执行由浅变深的透明变化
			AlphaAnimation fade_in = new AlphaAnimation(0.0f, 1);
			fade_in.setDuration(100);
			as.addAnimation(fade_in);
		}
		if (as != null) {
			ALLog.d("as" + as.getDuration());

			base.startAnimation(as);
		}
		mMytoast = toast;

	}

	public static void dismissToast(MyToast toast) {
		if (toast == null) {
			ALLog.e("error!your param is null!");
			return;
		}
		ALLog.d("remove toast");
		final View v = toast.getBaseLayour();
		// 执行由深变浅的动画
		AlphaAnimation fade_out = new AlphaAnimation(1, 0.0f);
		fade_out.setDuration(100);
		v.clearAnimation();
		v.startAnimation(fade_out);
		fade_out.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				v.setVisibility(View.GONE);
			}
		});
	}
	
	
	//2014-3-5 新增
	
	private static Toast mToast = null;
	private static MyToastUtil instance = null;

	/**
	 * 创建实例
	 * 
	 * @author Alonso Lee
	 */
	public static MyToastUtil getInstance() {
		if (instance == null) {
			new MyToastUtil();
		}
		return instance;
	}

	public MyToastUtil() {
		mToast = Toast.makeText(ALLocalEnv.getLocalEnv().getApplicationContext(), "", Toast.LENGTH_SHORT);
		instance = this;
	}

	/**
	 * 在屏幕上方打出提示
	 * 
	 * @param msg
	 *            提示信息
	 * @author Alonso Lee
	 */
	public void showToastOnTop(final String msg) {
		mToast.setGravity(Gravity.TOP, 0, 0);
		mToast.setText(msg);
		mToast.show();
	}

	/**
	 * 在屏幕中间打出提示
	 * 
	 * @param msg
	 *            提示信息
	 * @author Alonso Lee
	 */
	public void showToastOnCenter(final String msg) {
		mToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
		mToast.setText(msg);
		mToast.show();
	}

	/**
	 * 在屏幕下方打出提示
	 * 
	 * @param msg
	 *            提示信息
	 * @author Alonso Lee
	 */
	public void showToastOnButtom(final String msg) {
		mToast.setGravity(Gravity.BOTTOM, 0, 0);
		mToast.setText(msg);
		mToast.show();
	}

	public void showCustomToastOnCenter(View v) {
		mToast.setView(v);
		mToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
		mToast.show();
	}

	public void showCustomToastOnCenter(String msg) {
		try {
			View v = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater().inflate(R.layout.custom_center_toast, null);
			TextView text = (TextView) v.findViewById(R.id.text);
			v.findViewById(R.id.postive_icon).setVisibility(View.VISIBLE);
			text.setText(msg);

			Toast toast = Toast.makeText(ALLocalEnv.getLocalEnv().getApplicationContext(), "", Toast.LENGTH_SHORT);

			toast.setView(v);
			toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
			toast.show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void showCenterToastOnCenter(String msg) {
		try {
			View v = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater().inflate(R.layout.custom_center_toast, null);
			TextView text = (TextView) v.findViewById(R.id.text);
			text.setText(msg);

			Toast toast = Toast.makeText(ALLocalEnv.getLocalEnv().getApplicationContext(), "", Toast.LENGTH_SHORT);

			toast.setView(v);
			toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
			toast.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void showCenterToastOnCenter(int res) {
		try {
			View v = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater().inflate(R.layout.custom_center_toast, null);
			TextView text = (TextView) v.findViewById(R.id.text);
			text.setText(ALLocalEnv.getLocalEnv().getText(res));

			Toast toast = Toast.makeText(ALLocalEnv.getLocalEnv().getApplicationContext(), "", Toast.LENGTH_SHORT);

			toast.setView(v);
			toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
			toast.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
